﻿using System;
using System.Linq;
using System.Web.Mvc;
using System.Web.Security;
using APMS.WebSite.Common;
using APMS.WebSite.Models.DataStore;
using APMS.WebSite.Models.ViewData.Pages;

namespace APMS.WebSite.Controllers
{
    public class Writeboard_commentController : BaseController
    {
        public ActionResult Index()
        {
            // Add action logic here
            throw new NotImplementedException();
        }
        //writeboard route
        public ActionResult create(int projectId, int writeboardId, String comment_detail)
        {
            //throw new NotImplementedException();   
            String username = "admin";
            MySQLDataStore db = DataStoreFactory.GetMySQLDataStore();
            WriteBoardComment newWriteBoardComment = new WriteBoardComment();
            newWriteBoardComment.CommentDetail = comment_detail;
            newWriteBoardComment.WriteBoardVersionID = 1;//max version
            newWriteBoardComment.ModifyDate = DateTime.Now;
            newWriteBoardComment.AddDate = DateTime.Now;


            db.WriteBoardComment.Add(newWriteBoardComment);
            db.SubmitChanges();
            return View("test");
        }

        public ActionResult Delete(int projectId, int writeboardId, int id)
        {
            //throw new NotImplementedException();
            MySQLDataStore db = DataStoreFactory.GetMySQLDataStore();


            try
            {
                WriteBoardComment deleteWriteBoardComment = db.WriteBoardComment.Single(p => p.WriteBoardCommentID == id);
                db.WriteBoardComment.Remove(deleteWriteBoardComment);
                db.SubmitChanges();
            }
            catch (InvalidOperationException ex)
            {
                System.Console.WriteLine("called on set with zero items");
            }
            ////////////////////// DELETE DATA

            return Index();

            return View("test");
        }



        public ActionResult Update(int projectId, int id)
        {
            // Initialize data required by master page

            ActionResult exception = base.InitializeAction();
            if (exception != null)
                return exception;

            // Ser Window Title
            base.SetTitle("WriteBoard Comment Item | " + id);

            // SELECT * FROM user WHERE user.UserName = '{currentUser}'
            MySQLDataStore db = DataStoreFactory.GetMySQLDataStore();
            var query =
                from writeBoardComment in db.WriteBoardComment
                where
                    writeBoardComment.WriteBoardCommentID == id
                select writeBoardComment;

            // Prepare ViewData
            ManageWriteboard_commentViewData viewData = new ManageWriteboard_commentViewData();
            viewData.CurrentWriteboard_comment = query.First<WriteBoardComment>();

            //TODO: Handle both POST and GET methods
            if (Request.HttpMethod == Constants.HttpMethod.POST)
            {
                // If POST means UPDATE the value from Request.Form to DB
                // If GET means VIEW the value from DB
                #region Update record back to DB
                viewData.CurrentWriteboard_comment.CommentDetail = Request.Form["CommentDetail"];
                viewData.CurrentWriteboard_comment.ModifyDate = DateTime.Now;

                viewData.IsCurrentWriteboard_commentUpdate = true;

                db.SubmitChanges();
                #endregion
            }
            else
            {
                viewData.IsCurrentWriteboard_commentUpdate = false;
            }

            return View("Update", viewData);
        }

        public ActionResult List(int projectId, int writeboardId)
        {
            MySQLDataStore db = DataStoreFactory.GetMySQLDataStore();
            var query =
                 from writeBoardComment in db.WriteBoardComment
                 where
                     writeBoardComment.WriteBoardID == writeboardId
                 select writeBoardComment;

            WriteBoard writeBoard = db.WriteBoard.Single(p => p.WriteBoardID == writeboardId);

            ViewData["writeBoard"] = writeBoard;
            ViewData["all"] = query.ToList();

            return View("test");
        }

    }
}
